#include <Arduino.h>
#include <cstring>
#include <cstdio>
#include "../include/HC595_Driver.h"
#include "../include/Wifi_Driver.h"
#include "../include/TrafficState.h"

bool connect = 0;
TrafficState trafficState;
const int Digit[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
int clientid = 1;

void WriteState()
{
  for (int i = 0; i < 4; i++)
  {
    int time = trafficState.TrafficTimer[i];
    if (trafficState.TrafficTimer[i] > 99)
    {
      time = 99;
    }
    if (trafficState.TrafficTimer[i] < 0)
    {
      time = 0;
    }
    if (trafficState.LightState[i][0])
    {
      digitalWrite(Pin[i][0], HIGH);
    }
    else
    {
      digitalWrite(Pin[i][0], LOW);
    }
    int temp = 0;
    temp |= (trafficState.LightState[i][1] << 15);
    temp |= (trafficState.LightState[i][2] << 14);
    temp |= (Digit[time % 10] << 7);
    temp |= Digit[time / 10];
    Write_595(i, temp);
  }
  for (int i = 0; i < 4; i++)
  {
    Sync_595(i);
  }
}

void ControlTrafficLight(void *param)
{
  Init(&trafficState, clientid);
  char data[256];
  while (1)
  {
    // Serialize(&trafficState, data);
    // Serial.print(data);
    WriteState();
    UpdateAll(&trafficState);
    delay(1000);
  }
}

void ControlState(void *param)
{
  char data[256];
  while (1)
  {
    Serial.println("尝试访问服务器");
    if (client.connect(serverIP, serverPort)) // 尝试访问目标地址
    {
      connect = 1;
      Serial.print("TCP Suc\n");
      Serial.println("访问成功");
      client.print("Hello world!");                    // 向服务器发送数据
      while (client.connected() || client.available()) // 如果已连接或有收到的未读取的数据
      {
        if (client.available()) // 如果有数据可读取
        {
          String line = client.readStringUntil('\n'); // 读取数据到换行符
          memset(data, 0, sizeof(data));
          line.toCharArray(data, sizeof(data));
          Serial.print(data);

          if (strncmp(data, "ASK", 3) == 0)
          {
            memset(data, 0, sizeof(data));
            sprintf(data, "%d\n", clientid);
            client.write(data);
          }
          else if (strncmp(data, "SNYC", 4) == 0)
          {
            memset(data, 0, sizeof(data));
            Serialize(&trafficState, data);
            client.write(data);
          }
          else if (strncmp(data, "CHANGE", 6) == 0)
          {
            line = client.readStringUntil('\n');
            memset(data, 0, sizeof(data));
            line.toCharArray(data, sizeof(data));
            if (strlen(data) > 10)
            {
              Deserialize(&trafficState, data);
            }
          }
        }
      }

      connect = 0;
      // Serial.println("关闭当前连接");
      client.stop(); // 关闭客户端
    }
    else
    {
      // Serial.println("访问失败");
      client.stop(); // 关闭客户端
      connect = 0;
    }
  }
}

void setup()
{
  Init_595();
  Serial.begin(115200);
  delay(100);

  Wifi_init();
}

void loop()
{
  xTaskCreatePinnedToCore(ControlTrafficLight, "ControlTrafficLight", 4096, NULL, 1, NULL, 0);
  xTaskCreatePinnedToCore(ControlState, "ControlState", 4096, NULL, 2, NULL, 1);
  while (1)
    ;
}